import * as fs from "fs";
import {HtmlCode} from "./html";
import  * as path from "path"
import {GenKt} from "./kt";
import {Menu} from "../expressExt/Menu";
import {Config} from "./Config";


export class GenCfg {
  

    //接口代码路径
    static routerMenu = ["api", "routes"];

    //代码输出路径
    static codeOutMenu = "runtime/genCode";

    //接口个数统计
    static genCount = 0;

    //用于储存routerMenu的所有ts代码
    static expFuncStr = "";

    static ExpressExtCommentPos = 5;
    //static ExpressExtCommentPos = 7;

    static start(url:string, modReq:{ new():any;}, resp:any, method: string) {


        if (!Config.enableHTML)
            return;

        if (resp == null)
            return;

        if (!fs.existsSync(GenCfg.codeOutMenu)) {
            fs.mkdirSync(GenCfg.codeOutMenu)
        }

        GenCfg.genCount++;

        //读取routerMenu的所有ts代码
        if (GenCfg.expFuncStr.length == 0) {
            for (let u of GenCfg.routerMenu) {
                let m = path.join(Menu.root, u);
                let ffs = fs.readdirSync(m);
                for (let f of ffs) {
                    if (f.substring(f.length - 3) != ".ts")
                        continue;
                    // console.error("genCPPfile : " + m + f);
                    GenCfg.expFuncStr += fs.readFileSync(m + "/" + f) + "\r\n";
                }
            }
        }

        HtmlCode.genFile(url, modReq, resp, method == "get" ? "false" : "true");
        GenKt.genFile(url, modReq, resp, method == "get" ? "false" : "true");
    }
}